﻿@using System.Text.Json
@using FormBuilder.Components.Shared
@inject DialogService dialogService

<RadzenStack>
    <RadzenTabs @bind-SelectedIndex=@selectedIndex>
        <Tabs>
            <RadzenTabsItem Text="Properties">
                <RadzenStack>
                    <!-- Can view the password -->
                    <div>
                        <RadzenCheckBox @bind-Value="CopyRecord.CanViewPassword" />
                        <RadzenLabel>Can view the password ?</RadzenLabel>
                    </div>
                </RadzenStack>
            </RadzenTabsItem>
            <RadzenTabsItem Text="Label">
                <EditLabelsComponent Labels="@CopyRecord.Labels" />
            </RadzenTabsItem>
        </Tabs>
    </RadzenTabs>
    <RadzenButton Text="Save" Click="@HandleSave"></RadzenButton>
</RadzenStack>

@code {
    public int selectedIndex { get; set; } = 0;
    FormPasswordFieldRecord CopyRecord { get; set; } = new FormPasswordFieldRecord();
    [Parameter] public FormPasswordFieldRecord Record { get; set; }
    [Parameter] public WorkflowContext WorkflowContext { get; set; }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (Record != null)
        {
            CopyRecord = JsonSerializer.Deserialize<FormPasswordFieldRecord>(JsonSerializer.Serialize(Record));
        }
    }

    private void HandleSave()
    {
        Record.Labels = CopyRecord.Labels;
        Record.CanViewPassword = CopyRecord.CanViewPassword;
        dialogService.Close();
    }
}